<template>
  <div class="collection-file">
    <div class="gva-btn-list">
      <el-button icon="plus" size="small" type="primary" @click="openDialog('addApi')">新增</el-button>
      <el-popover v-model="deleteVisible" placement="top" width="160">
        <p>确定要删除吗？</p>
        <div style="text-align: right; margin-top: 8px;">
          <el-button link size="small" type="primary" @click="deleteVisible = false">取消</el-button>
          <el-button size="small" type="primary" @click="onDelete">确定</el-button>
        </div>
        <template #reference>
          <el-button
            :disabled="!deleteIds.length"
            icon="delete"
            size="small"
            style="margin-left: 10px;"
            @click="deleteVisible = true"
          >删除
          </el-button>
        </template>
      </el-popover>
    </div>
    <div class="gva-table-box">
      <el-table :data="tableData" @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          width="55"
        />
        <el-table-column align="left" label="id" min-width="60" prop="id" />
        <el-table-column v-slot="scope" align="left" label="类型" min-width="60">
          <span v-if="scope.row.type === 10">主文件</span>
          <span v-if="scope.row.type === 20">子文件</span>
          <span v-if="scope.row.type === 30">封面图</span>
        </el-table-column>
        <el-table-column v-slot="scope" align="left" label="文件" min-width="60">
          <img
            v-if="imagesSuffixList.includes(scope.row.fileUrl.split('.').pop())"
            :height="100"
            :src="scope.row.fileUrl"
            alt=""
          >
          <a :href="scope.row.fileUrl">
            <span v-if="!imagesSuffixList.includes(scope.row.fileUrl.split('.').pop())">
              {{ scope.row.fileUrl.split('/').pop() }}
            </span>
          </a>
        </el-table-column>
        <el-table-column v-slot="scope" label="操作">
          <el-button
            icon="edit"
            link
            size="small"
            type="primary"
            @click="editCollectionFileFunc(scope.row.id)"
          >编辑
          </el-button>
          <el-button
            icon="delete"
            link
            size="small"
            type="primary"
            @click="deleteCollectionFileFunc(scope.row.id)"
          >删除
          </el-button>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog v-if="dialogFormVisible" v-model="dialogFormVisible" :before-close="closeDialog" :title="dialogTitle">
      <edit-collection-file :detail="detail" @editRes="editRes" />
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'CollectionFile',
}
</script>

<script setup>

import { ref } from 'vue'
import {
  collectionFileDetail,
  collectionFileList,
  deleteCollectionByIds,
  deleteCollectionFile,
  deleteCollectionFileByIds
} from '@/api/collection'
import { useRoute } from 'vue-router'
import EditCollectionFile from '@/view/collection/collectionFile/edit.vue'
import { ElMessageBox, ElMessage } from 'element-plus'

const imagesSuffixList = ['jpg', 'png', 'jpeg', 'gif', 'webp']

const route = useRoute()
const tableData = ref([])
const deleteIds = ref([])

const getTableData = async() => {
  const res = await collectionFileList({ collectionId: route.query.collectionId })
  if (res.code === 200) {
    tableData.value = res.data.list
  }
}

const handleSelectionChange = (val) => {
  deleteIds.value = val.map((item) => item.id)
}

const editCollectionFileFunc = async(id) => {
  // 获取藏品文件详情
  const res = await collectionFileDetail({ id: id })
  if (res.code === 200) {
    detail.value = res.data
  }

  dialogFormVisible.value = true
  dialogTitle.value = '编辑藏品文件'
}

// 删除藏品文件
const deleteCollectionFileFunc = async(id) => {
  ElMessageBox.confirm('此操作将永久删除该藏品, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async() => {
    const res = await deleteCollectionFile({ id: id })
    if (res.code === 200) {
      ElMessage({
        type: 'success',
        message: '删除成功!'
      })
      if (tableData.value.length === 1 && page.value > 1) {
        page.value--
      }
      await getTableData()
    }
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消删除'
    })
  })
}

const dialogTitle = ref('新增藏品文件')
const dialogFormVisible = ref(false)
const detail = ref({
  collectionId: route.query.collectionId,
})

const openDialog = () => {
  dialogFormVisible.value = true
}

const onDelete = async() => {
  const res = await deleteCollectionFileByIds({ ids: deleteIds.value })
  if (res.code === 200) {
    ElMessage({
      type: 'success',
      message: '删除成功！'
    })

    if (tableData.value.length === deleteIds.length && page.value > 1) {
      page.value--
    }
    await getTableData()
  }
}

const closeDialog = () => {
  detail.value = {}
  dialogFormVisible.value = false
}

const editRes = () => {
  detail.value = {
    collectionId: route.query.collectionId,
  }
  dialogFormVisible.value = false
  getTableData()
}

getTableData()

</script>
